<template>
  <div
    ref="mergeDom"
    class="v-model-merge"
  >
    <div class="_merge-tool-bar g-flex g-fs-14 pd-m-tb-12">
      <MergeToolBar
        v-for="(item, index) in buttonList"
        :key="index"
        :instance-id="modelId"
        :owner-id="ownerId"
        :title="item.title"
        :buttons="item.buttons"
        :currentKey="currentKey"
        :selected-nodes="selectedNodes"
        @update:property="handleUpdateUpdatedIdList"
      />
    </div>

    <div class="g-flex _content">
      <div class="_left">
        <div class="_left-top">
          <MergeTree
            v-model:modelId="modelId"
            :owner-id="ownerId"
            :updated-ids="updatedIdList"
            :currentKey="currentKey"
            :propertyList="propertyList"
            @update-selected-nodes="handleSelectedNodeChange"
            @multiSelect="handleMultSelect"
            @update:model-id="handleUpdateModelId"
            @update:property="handleUpdateUpdatedIdList"
            @openDiagram="handleOpenDiagram"
            @updateButtonList="handleUpdateButtonList"
          />
        </div>
        <div class="_left-bottom">
          <div class="__window-bar g-flex g-h-28 g-flex-0">
            <div class="g-lh-14 _title">{{ $t("规则摘要") }}</div>
          </div>
          <MergeCount />
        </div>
      </div>

      <div class="_right">
        <div class="_right-top">
          <div class="__window-bar g-flex g-h-28 g-flex-0">
            <div class="g-lh-14 _title">{{ $t("属性对比") }}</div>
          </div>
          <MergePropertyList
            v-model:currentKey="currentKey"
            :property-list="propertyList"
            :instance-id="modelId"
            :isDisabled="isNodeDisabled"
            @updateButtonList="handleUpdateButtonList"
            @update:property="handleUpdateUpdatedIdList"
          />
        </div>
        <!-- <div  class="_right-bottom">
          <div
            class="__window-bar g-flex g-h-28 g-flex-0"
          >
            <div class="__move-bar g-h-16 g-m-l-16 g-m-r-8"></div>
            <div class="g-fs-16 g-bloder g-lh-16">变更详情</div>
          </div>
          <MergePropertyDetail/>
        </div> -->
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { getUid } from "@/util/common";
import { reactive, ref, inject } from "vue";
import MergeToolBar from "./MergeToolBar.vue";
import MergeTree from "./component/MergeTree.vue";
import MergeCount from "./component/MergeCount.vue";
import MergePropertyList from "./component/MergePropertyList.vue";
import { MergeModelData, SelectedNode } from "./component/logic";
import { buttonList as _buttonList, MergeProperty } from "./config";
import { projectService } from "../../service";

const targetProjectId = inject("targetProjectId") as string;
const sourceProjectId = inject("sourceProjectId") as string;
// watch(() => props.highLightId, (val, oldVal) => {
//   if (val && val != oldVal) {
//     handleUpdateModelId(val, '');
//   }
// });
const emits = defineEmits<{
  (event: "openDiagram", modelId: string, data: MergeModelData): void;
}>();
const modelId = ref(""); // 注意，这里需要默认为根节点id
const ownerId = ref("");
const currentKey = ref("");
const propertyList = ref<Array<MergeProperty>>([]);
const updatedIdList = ref<string[]>([]);
const mergeDom = ref<HTMLDivElement>();
const buttonList = reactive(_buttonList);
const isNodeDisabled = ref(false);
const selectedNodes = ref<SelectedNode[]>([]);

function handleSelectedNodeChange(nodes: SelectedNode[]) {
  selectedNodes.value = nodes;
}

async function handleOpenDiagram(_modelId: string, data: MergeModelData) {
  emits("openDiagram", _modelId, data);
}
function handleUpdateButtonList(menuConfig: { [key: string]: boolean }) {
  if (isNodeDisabled.value) {
    buttonList.forEach(button => {
      button.buttons.forEach(element => {
        element.disabled = true;
      });
    });
  } else {
    buttonList.forEach(button => {
      button.buttons.forEach(element => {
        element.disabled = menuConfig[element.key];
      });
    });
  }
}

// 更新modelId和propertyList
async function handleUpdateModelId(id: string, parentId: string, disabled: boolean, res?: MergeProperty[]) {
  if (id.includes("-relations")) {
    propertyList.value = [];
    return;
  }
  let list: MergeProperty[] = [];
  if (res) {
    list = res;
  } else {
    let result = await projectService.projectMergeQueryPropertyDiff(targetProjectId, sourceProjectId, id);
    list = result.data;
  }
  isNodeDisabled.value = disabled;
  currentKey.value = "";
  modelId.value = id;
  ownerId.value = parentId;
  propertyList.value = list.map(it => {
    return {
      ...it,
      uid: getUid()
    };
  });
}

function handleMultSelect() {
  propertyList.value = [];
}

// modelId不变，更新updatedIdList和propertyList
async function handleUpdateUpdatedIdList(ids: string[]) {
  if (modelId.value.indexOf("relations") < 0) {
    const list = await projectService.projectMergeQueryPropertyDiff(targetProjectId, sourceProjectId, modelId.value);
    propertyList.value = list.data.map(it => {
      return {
        ...it,
        uid: getUid()
      };
    });
  }
  updatedIdList.value = ids;
}
</script>

<style lang="scss" scoped>
@import "@/assets/css/theme.scss";
$border: 1px solid #d0d0d0;
.v-model-merge {
  padding: 0 26px 26px 26px;
  height: 100%;
  background-color: var(--md-color-sub3);
  display: flex;
  flex-direction: column;
  ._merge-tool-bar {
    height: 40px;
    align-items: center;
    flex: 0 0 auto;
  }
  .__window-bar {
    background-color: $toolbar-bg-color;
    border-bottom: $border;
    align-items: center;
    ._title {
      margin-left: 12px;
      border-left: 3px solid $theme-color;
      padding-left: 12px;
      font-size: 12px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: bold;
      color: rgba(0, 0, 0, 0.85);
      text-align: center;
    }
    .__move-bar {
      width: 5px;
      background-color: $theme-color;
    }
  }
  ._content {
    flex: 1 1 auto;
    overflow: hidden;
  }
  ._left {
    flex: 1 1 1px;
    overflow: hidden;
    display: flex;
    flex-direction: column;

    ._left-top {
      flex: 1 1 1px;
      overflow: auto;
      border: 1px solid #c4c1c2;
      background: #fff;
    }
    ._left-bottom {
      overflow: auto;
      border: 1px solid #c4c1c2;
      background: #fff;
    }
  }
  ._right {
    flex: 1 1 1px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    border: 1px solid #c4c1c2;
    border-left: none;
    ._right-top {
      flex: 1 1 1px;
      overflow: auto;
    }
    ._right-bottom {
      flex: 1 1 1px;
      overflow: auto;
    }
  }
  // .vue-grid-item {
  //   &:not(.vue-grid-placeholder) {
  //     border: $border;
  //   }
  // }
  // :deep(.vue-grid-item.vue-grid-placeholder) {
  //   background: #C5E2FD;
  // }
}
</style>
